package com.demo.service;
import com.demo.bean.User;
import com.demo.bean.vo.ResponseResult;
import com.demo.config.DatabaseConnection;
import com.demo.dao.UserDao;
import java.sql.Connection;
import java.util.List;

public class UserService {
  private final UserDao userDao = new UserDao();

  public ResponseResult getAllUsers() {
    try (Connection conn = DatabaseConnection.getConnection()) {
      List<User> users = userDao.selectAllUsers(conn);
      return new ResponseResult(200, "Success", users);
    } catch (Exception e) {
      return new ResponseResult(500, "Error: " + e.getMessage(), null);
    }
  }

  public ResponseResult getUserById(String userId) {
    try (Connection conn = DatabaseConnection.getConnection()) {
      User user = userDao.selectUserByUserId(conn, userId);
      if (user != null) {
        return new ResponseResult(200, "Success", user);
      }
      return new ResponseResult(404, "User not found", null);
    } catch (Exception e) {
      return new ResponseResult(500, "Error: " + e.getMessage(), null);
    }
  }
}
